package com.ruoyi.quality.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.quality.domain.TQualityFile;
import com.ruoyi.safe.domain.BizSafeAttach;
import com.ruoyi.safe.service.impl.BizSafeAttachServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.quality.mapper.TQualityQuestionsMapper;
import com.ruoyi.quality.domain.TQualityQuestions;
import com.ruoyi.quality.service.ITQualityQuestionsService;

/**
 * 问题规范库Service业务层处理
 * 
 * @author lxg
 * @date 2024-02-02
 */
@Service
public class TQualityQuestionsServiceImpl implements ITQualityQuestionsService 
{
    @Value("${minio.bucketName}")
    private String bucketName;
    @Autowired
    private TQualityQuestionsMapper tQualityQuestionsMapper;
    @Autowired
    private BizSafeAttachServiceImpl bizSafeAttachService;

    /**
     * 查询问题规范库
     * 
     * @param id 问题规范库主键
     * @return 问题规范库
     */
    @Override
    public TQualityQuestions selectTQualityQuestionsById(Long id)
    {
        TQualityQuestions t = tQualityQuestionsMapper.selectTQualityQuestionsById(id);
        if (t != null)
        {
            if(StringUtils.isNotNull(t.getPics()) && !"".equals(t.getPics())){
                String[] arr = t.getPics().split(",");
                Long[] photoIds = new Long[arr.length];
                for(int j=0;j<arr.length;j++){
                    photoIds[j] = Long.parseLong(arr[j]);
                }
                List<BizSafeAttach> bizSafeAttaches = bizSafeAttachService.selectBizSafeAttachByBizIds(photoIds);
                t.setPicsArr(bizSafeAttaches);
            }
        }
        return t;
    }

    /**
     * 查询问题规范库列表
     * 
     * @param tQualityQuestions 问题规范库
     * @return 问题规范库
     */
    @Override
    public List<TQualityQuestions> selectTQualityQuestionsList(TQualityQuestions tQualityQuestions)
    {
        List<TQualityQuestions> list =  tQualityQuestionsMapper.selectTQualityQuestionsList(tQualityQuestions);
        for (TQualityQuestions item : list)
        {
            item.setBucket(bucketName);
        }
        return list;
    }
    @Override
    public List<TQualityQuestions> selectTQualityQuestionsList1()
    {
        List<TQualityQuestions> list =  tQualityQuestionsMapper.selectTQualityQuestionsList1();
        for (TQualityQuestions item : list)
        {
            item.setBucket(bucketName);
        }
        return list;
    }

    /**
     * 新增问题规范库
     * 
     * @param tQualityQuestions 问题规范库
     * @return 结果
     */
    @Override
    public int insertTQualityQuestions(TQualityQuestions tQualityQuestions)
    {
        tQualityQuestions.setCreateTime(DateUtils.getNowDate());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tQualityQuestions.setCreateBy(loginUser.getUserId().toString());
        String pics = "";
        if(tQualityQuestions.getPic()!=null && tQualityQuestions.getPic().size()>0){
            for(String picId : tQualityQuestions.getPic()){
                pics += picId +",";
            }
            pics = pics.substring(0,pics.length()-1);
            tQualityQuestions.setPics(pics);
        }

        return tQualityQuestionsMapper.insertTQualityQuestions(tQualityQuestions);
    }

    /**
     * 修改问题规范库
     * 
     * @param tQualityQuestions 问题规范库
     * @return 结果
     */
    @Override
    public int updateTQualityQuestions(TQualityQuestions tQualityQuestions)
    {
        tQualityQuestions.setUpdateTime(DateUtils.getNowDate());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tQualityQuestions.setUpdateBy(loginUser.getUserId().toString());
        String pics = "";
        if(tQualityQuestions.getPic()!=null && tQualityQuestions.getPic().size()>0){
            for(String picId : tQualityQuestions.getPic()){
                pics += picId +",";
            }
            pics = pics.substring(0,pics.length()-1);
            tQualityQuestions.setPics(pics);
        }
        return tQualityQuestionsMapper.updateTQualityQuestions(tQualityQuestions);
    }

    /**
     * 批量删除问题规范库
     * 
     * @param ids 需要删除的问题规范库主键
     * @return 结果
     */
    @Override
    public int deleteTQualityQuestionsByIds(Long[] ids)
    {
        return tQualityQuestionsMapper.deleteTQualityQuestionsByIds(ids);
    }

    /**
     * 删除问题规范库信息
     * 
     * @param id 问题规范库主键
     * @return 结果
     */
    @Override
    public int deleteTQualityQuestionsById(Long id)
    {
        return tQualityQuestionsMapper.deleteTQualityQuestionsById(id);
    }
}
